package command;

import parser.ChessContentHandlerConstants;
import parser.ParserConstants;

public class ChessMoveCountCommand implements Command {

	private int origCount,endCount;
	private game.ChessGame currentGame;
	private Command secondaryCommand;
	
	/**
	 * Constructor for the ChessMoveCountCommand class.  This class
	 * can be used to keep track of how many moves it has been since
	 * a pawn move or an attack, for the 50 moves rule in chess.
	 * 
	 * And yes, this class does directly require a ChessGame, so don't
	 * even try and use this for your Eight Queens or Knights Tour game.
	 * 
	 * <pre>
	 * Max Joyce
	 * -Creation (Mar 22, 2010)
	 * </pre>
	 * @param origCount
	 * @param newCount
	 * @param curGame
	 */
	public ChessMoveCountCommand(int origCount, int newCount, game.ChessGame curGame){
		this.origCount=origCount;
		this.endCount=newCount;
		this.currentGame=curGame;
		this.secondaryCommand=new NoSecondaryCommand();
	}
	
	@Override
	public void addSecondaryCommand(Command secondaryCommand) {
		if (this.secondaryCommand instanceof NoSecondaryCommand){
			this.secondaryCommand = secondaryCommand;
		}
		else{
			this.secondaryCommand.addSecondaryCommand(secondaryCommand);
		}
		
	}

	@Override
	public void execute() {
		this.currentGame.setMoveCounter(endCount);
		this.secondaryCommand.execute();
		
	}

	@Override
	public String exportCode() {
		// TODO Finish this, Caleb!
		return "";
	}

	@Override
	/*
	 * This isn't really applicable to this command...
	 */
	public boolean hadMovedPreviously() {
		return false;
	}
	public int getEndCount(){
		return endCount;
	}
	@Override
	public void undo() {
		this.secondaryCommand.undo();
		this.currentGame.setMoveCounter(origCount);
		
	}
	@Override
	public String xmlCode() {
		String code = "<" + ParserConstants.ELEMENT_MOVE_CHANGE + ">\n";
		code += "\t" + covertCountToPoint(ChessContentHandlerConstants.POINT_1).replaceAll("\n", "\n\t");
		code += "\n";
		code += "\t" + XMLPieceTag().replaceAll("\n", "\n\t");
		code += "\n";
		code += "</" + ParserConstants.ELEMENT_MOVE_CHANGE + ">\n";
		code += secondaryCommand.xmlCode();
		return code;
	}
	/**
	 * This method is a helper method to turn the piece object into an XML string
	 * @return Returns a string representing the piece
	 */
	private String XMLPieceTag(){
		String s = "<" + ChessContentHandlerConstants.ELEMENT_GAME_TYPE + " ";
		s += ChessContentHandlerConstants.ATTRIBUTE_VALUE + "='" + ChessContentHandlerConstants.GAME_TYPE_CHESS + "'>";
		s += "\n</" + ChessContentHandlerConstants.ELEMENT_GAME_TYPE + ">";
		return s;
	}
	
	private String covertCountToPoint(String type){
		String s = "<" + ChessContentHandlerConstants.ELEMENT_COUNTER + " ";
		s += ChessContentHandlerConstants.ATTRIBUTE_TYPE + "='" + type + "'>\n";
		
		s += "\t" + "<" + ChessContentHandlerConstants.ELEMENT_START_COUNT + " ";
		s += ChessContentHandlerConstants.ATTRIBUTE_VALUE + "='" + origCount + "'>\n";
		s += "\t" + "</" + ChessContentHandlerConstants.ELEMENT_START_COUNT + ">\n";
		
		s += "\t" + "<" + ChessContentHandlerConstants.ELEMENT_FINAL_COUNT + " ";
		s += ChessContentHandlerConstants.ATTRIBUTE_VALUE + "='" + endCount + "'>\n";
		s += "\t" + "</" + ChessContentHandlerConstants.ELEMENT_FINAL_COUNT + ">\n";
		
		s += "</" + ChessContentHandlerConstants.ELEMENT_COUNTER + ">";
		return s;
	}

	@Override
	public Command getSecondaryCommand() {
		return secondaryCommand;
	}

}
